﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SharePointPowerToys
{
    public class LinkSharePointEditorPartControl : Control, INamingContainer
    {
        public string Header { get; set; }
        public string Body { get; set; }

        private TextBox linkTextBox = new TextBox { CssClass = "UserInput", Width = 176 };
        public string Link
        {
            get
            {
                return linkTextBox.Text;
            }
            set
            {
                linkTextBox.Text = value;
            }
        }
        public bool BuilderVisible { get; set; }

        public LinkSharePointEditorPartControl()
        {
        }

        public LinkSharePointEditorPartControl(string header)
        {
            Header = header;
            Body = string.Empty;
            BuilderVisible = false;
        }

        public LinkSharePointEditorPartControl(string header, string body)
        {
            Header = header;
            Body = body;
            BuilderVisible = false;
        }

        public LinkSharePointEditorPartControl(string header, string body, string link)
        {
            Header = header;
            Body = body;
            Link = link;
            BuilderVisible = false;
        }

        public LinkSharePointEditorPartControl(string header, string body, string link, bool builderVisible)
        {
            Header = header;
            Body = body;
            Link = link;
            BuilderVisible = builderVisible;
        }
        
        protected override void CreateChildControls()
        {
            try
            {
                Page.ClientScript.RegisterClientScriptInclude("xtp", "/_layouts/1033/xtp.js");

                //Controls.Add(new LiteralControl("<tr>"));
                //Controls.Add(new LiteralControl("<td>"));
                Controls.Add(new LiteralControl("<div class=\"UserSectionHead ms-bold\">"));
                Controls.Add(new LiteralControl(Header));
                Controls.Add(new LiteralControl("</div>"));

                Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
                Controls.Add(new LiteralControl(Body));
                Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
                Controls.Add(new LiteralControl("<nobr>"));


                linkTextBox.ID = String.Concat(ID, "TextBox");
                linkTextBox.Text = Link;

                Controls.Add(linkTextBox);

                Button builderButton = new Button();

                if (BuilderVisible)
                {
                    builderButton.ID = String.Concat(ID, "BuilderButton");
                    builderButton.Text = "...";
                    //builderButton.Attributes.Add("title", "Click to use builder");
                    builderButton.ToolTip = "Click to use builder";
                    builderButton.CssClass = "ms-PropGridBuilderButton";

                    Controls.Add(builderButton);

                    //builderButton.Attributes.Add("class", "ms-PropGridBuilderButton");
                    //builderButton.Style.Add("display", "display:inline");
                    //onclick=\"javascript:MSOPGrid_doBuilder('\u002f_layouts\u002fzoombldr.aspx?culture=en-US', ctl00_MSOTlPn_EditorZone_Edit0g_4a9e08b1_a0de_4933_90ea_7a49511e2cd4_MsoXmlToolpartBasicxsllink, 'dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes');\"
                    builderButton.Attributes.Add("onclick", String.Concat("javascript:MSOPGrid_doBuilder('\u002f_layouts\u002fzoombldr.aspx?culture=en-US', ", linkTextBox.ClientID.ToString(), ", 'dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes');return false;"));

                    //textBox.Attributes.Add("onfocusout", "this.style.display='none';");

                    //Add attributes to the text box to show and hide the button when the

                    //control come in or out of focus or deactivated - just like SharePoint!

                    //textBox.Attributes.Add("onfocusin",

                    //     string.Format("MSOPGrid_BuilderVisible({0})", builderButton.ClientID.ToString()));

                    linkTextBox.Attributes.Add("onfocusin", string.Concat("MSOPGrid_BuilderVisible(", builderButton.ClientID.ToString(), ")"));

                    builderButton.Attributes.Add("onfocusout", "this.style.display='none';");

                    linkTextBox.Attributes.Add("ondeactivate",

                         string.Format("MSOTlPn_prevBuilder={0};", builderButton.ClientID.ToString()));


                    //Microsoft.SharePoint.WebControls.InputFormTextBox linkField = new Microsoft.SharePoint.WebControls.InputFormTextBox();
                    //Controls.Add(linkField);
                }

                Controls.Add(new LiteralControl("</nobr><br/>"));

                HyperLink testHyperLink = new HyperLink();
                testHyperLink.Text = "Test Link";
                testHyperLink.ToolTip = "Test Link (new window)";
                testHyperLink.NavigateUrl = String.Concat(Context.Request.Url, "#");
                testHyperLink.Attributes.Add("onclick", String.Concat("javascript:MsoFrameworkToolpartPreview(document.forms[MSOWebPartPageFormName].", linkTextBox.ClientID.ToString(), ".value,'The URL is not valid for the XSL Link property. Check the spelling and syntax, and then type a valid URL.',12);"));
                //onclick=\"javascript:MsoFrameworkToolpartPreview(document.forms[MSOWebPartPageFormName].ctl00_MSOTlPn_EditorZone_Edit0g_4a9e08b1_a0de_4933_90ea_7a49511e2cd4_MsoXmlToolpartBasicxsllink.value,'The URL is not valid for the XSL Link property. Check the spelling and syntax, and then type a valid URL.',12);\" 
                Controls.Add(testHyperLink);

                Controls.Add(new LiteralControl("</div>"));
                Controls.Add(new LiteralControl("</div>"));

                //Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
                //Controls.Add(new LiteralControl("</td>"));
                //Controls.Add(new LiteralControl("</tr>"));
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(String.Concat("Error: ", ex.Message)));
            }
        }
    }
}
